// 代理到现有文化青浦云后台 ajax 服务
// 添加跨域支持

const http = require('http')
const Koa = require('koa')
const axios = require('axios')

const app = new Koa()

// 所有配置集中写到此处
const config = {
    listen: {
        port: 2009
    },
    // targetPrefix: 'http://performance.eke88.com',
    targetPrefix: 'http://wx.jbrsys.com',
    rawResponse: false
}

const PORT = config.listen.port || 60080

const server = http.createServer(app.callback())
server.listen(PORT, () => {
    console.log(`listening at port:${PORT}`)
})

// 保证返回永远有跨域支持
app.use(async(ctx, next) => {
    ctx.set('Access-Control-Allow-Origin', '*')
    await next()
})

// 获取请求体，解析成 js 数据，目前看必须是第一个中间件

const getBackendForm = axios.create({
    timeout: 15 * 60 * 1000,
    responseType: config.rawResponse ? 'arraybuffer' : 'json',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
})

const postBackendForm = axios.create({
    timeout: 15 * 60 * 1000,
    responseType: config.rawResponse ? 'arraybuffer' : 'json',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
})

const contentType = require('content-type')
const getRawBody = require('raw-body')

let counter = 0
app.use(async(ctx, next) => {
    counter += 1
    const reqNo = counter
    console.log(`\nrequest.${reqNo} ${ctx.method} ${ctx.request.url}`)

    let body
    ctx.response.contentType = 'application/x-www-form-urlencoded;charset=UTF-8'
    if (ctx.method === 'POST') {
        body = await getRawBody(ctx.req, {
            length: ctx.req.headers['content-length'],
            limit: '1mb',
            encoding: 'utf-8'
        })
    }
    console.log(ctx.method, body)

    const response = await (ctx.method === 'GET' ?
        getBackendForm.get(config.targetPrefix + ctx.request.url) :
        postBackendForm.post(config.targetPrefix + ctx.request.url, body))

    console.log(
        `\nresponse.${reqNo} ${ctx.method}${config.targetPrefix} ${ctx.request.url}`
    )
    console.log(response.data)

    ctx.body = response.data
    ctx.type = 'application/json; charset=utf-8'

    await next()
})